Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
matrix (array of arrays|bidimensional array) sum without loops. There are a better way?

I was thinking on a way to make matrix sum without have to use loops and I realize use Array.prototype.map() is a good way.

var multArray = [[1,2,3,4,5],[6,7,8,9,10]];
var multArray2=[[1,2,3,4,5],[6,7,8,9,10]];
total   =  multArray.map((array, arrayIndex) => array.map((value, index) => value + multArray2[arrayIndex][index]));
console.log(total);

Do you know if there are a better or short way?

Thanks!.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

"Better" is subjective, but you can do something like this:

var multArray = [[1,2,3,4,5],[6,7,8,9,10]];
var multArray2=[[1,2,3,4,5],[6,7,8,9,10]];
console.log(
  [...Array(multArray.length).keys()].map(
     f => [...Array(multArray[f].length).keys()].map(
        s => multArray[f][s] + multArray2[f][s]
     )
  )
)

But IMO your one is more readable

about 4 years ago · Juan Pablo Isaza Relatório

0

I think a simple for loop is the most explicit in terms of clearly showing what is happening.

const multArray = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],];
const multArray2 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],];

const total = [];

for (let i = 0; i < multArray.length; i++) {
  total[i] = [];
  for (let j = 0; j < multArray[i].length; j++) {
    total[i][j] = multArray[i][j] + multArray2[i][j];
  }
}

console.log(total);

But if you really don't want to you could define some utility functions and chain them.

const multArray = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],];
const multArray2 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],];

const zip = (a, b) => a.map((k, i) => [k, b[i]]);
const sum = (arr) => arr.reduce((a, b) => a + b, 0);

const total = zip(multArray, multArray2).map((c) => zip(...c).map(sum));

console.log(total);

A real advantage can be gained by redefining the zip() function to accept any number of parameters and thus allowing you to total any number of arrays by simply passing them all to the initial zip call (and avoiding explicitly writing nested loops for each additional array).

const multArray = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],];
const multArray2 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],];
const multArray3 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],];

const zip = (...rows) => [...rows[0].keys()].map((k) => rows.map((row) => row[k]));
const sum = (arr) => arr.reduce((a, b) => a + b, 0);

const sum_mult_arrays = (...arrs) => zip(...arrs).map((c) => zip(...c).map(sum));

const total = sum_mult_arrays(multArray, multArray2, multArray3);

console.log(total);

See Javascript equivalent of Python's zip function for a lot of discussion around zipping.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda